/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <native_drawing/drawing_error_code.h>
#include "utils/log.h"
#include "imageknife_brush.h"

namespace ImageKnifePro {

ImageKnifeBrush::ImageKnifeBrush()
{
    brush_ = OH_Drawing_BrushCreate();
    if (brush_ == nullptr) {
        IMAGE_KNIFE_LOG(LOG_ERROR, "OH_Drawing_BrushCreate failed!");
    }
    brushFilter_ = OH_Drawing_FilterCreate();
    if (brushFilter_ == nullptr) {
        IMAGE_KNIFE_LOG(LOG_ERROR, "OH_Drawing_FilterCreate failed!");
    }
}

ImageKnifeBrush::~ImageKnifeBrush()
{
    if (brush_ != nullptr) {
        OH_Drawing_BrushDestroy(brush_);
    }
    if (brushFilter_ != nullptr) {
        OH_Drawing_FilterDestroy(brushFilter_);
    }
}

OH_Drawing_Brush *ImageKnifeBrush::GetCanvasBrush()
{
    return brush_;
}

int ImageKnifeBrush::SetColorFilter(OH_Drawing_ColorFilter *colorFilter)
{
    OH_Drawing_ErrorCode errCode = OH_DRAWING_SUCCESS;

    if (brush_ == nullptr || brushFilter_  == nullptr || colorFilter == nullptr) {
        IMAGE_KNIFE_LOG(LOG_ERROR, "Brush,brushFilter_ or ColorFilter is NULL!");
        return -1;
    }

    OH_Drawing_FilterSetColorFilter(brushFilter_, colorFilter);

    if ((errCode=OH_Drawing_ErrorCodeGet()) != OH_DRAWING_SUCCESS) {
        IMAGE_KNIFE_LOG(LOG_ERROR, "OH_Drawing_FilterSetColorFilter failed, errno=%{public}d", errCode);
        return -1;
    }

    OH_Drawing_BrushReset(brush_);
    OH_Drawing_BrushSetFilter(brush_, brushFilter_);

    return 0;
}

}
